influxDB 2.x JS

您所在的位置:网站首页 measurement error的例子 influxDB 2.x JS

influxDB 2.x JS

2023-06-22 08:36| 来源: 网络整理| 查看: 265

  在influxDB的官网NODEJS-client示例中:

'use strict' import { InfluxDB, Point } from '@influxdata/influxdb-client' const url = process.env.INFLUX_URL const token = process.env.INFLUX_TOKEN const org = process.env.INFLUX_ORG const bucket = process.env.INFLUX_BUCKET const influxDB = new InfluxDB({ url, token }) const writeApi = influxDB.getWriteApi(org, bucket) writeApi.useDefaultTags({ region: 'west' }) /** * Create a point and write it to the buffer. **/ const point1 = new Point('temperature') .tag('sensor_id', 'TLM01') .floatField('value', 24.0) console.log(` ${point1}`) writeApi.writePoint(point1) /** * Flush pending writes and close writeApi. **/ writeApi.close().then(() => { console.log('WRITE FINISHED') })

只用到了 writePoint() 方法,这个方法一次只能写一条标准influxDB数据:

        mesurement, tag='tag' field='fieldvalue' timestamp

有没有什么方法可以一次写多条数据呢?肯定有!

在 WriteApi 的接口中,有一个方法叫做 writePoints() 的方法,看这个名字就感觉他一定可以帮助我们一次写入多条数据。只是这个方法处理的数据格式只能是这样的:

[ measurement,tag1='tag1' field1='fieldvalue1' timestamp, measurement,tag2='tag2' field2='fieldvalue2' timestamp, measurement,tag3='tag3' field3='fieldvalue3' timestamp, measurement,tag4='tag4' field4='fieldvalue4' timestamp, .........]

首先,他得是个数组,其次,数组中的每一个元素,都必须包含measurement,tag,field,(timestamp)(timestamp可省略)。这样的数据才能写入influxdb数据库中。

示例:

const token = '***********' const url = 'http://localhost:8086' const org = '***' const bucket = '***' const writeclient = new InfluxDB({url, token}).getWriteApi(org, bucket) const Points = [] const Data1 = new Point(measurement) Data1.tag('tag1', tag1) Data1.intfield('field1', field1) Points.push(Data1) const Data2 = new Point(measurement) Data2.tag('tag2', tag2) Data2.intfield('field2', field2) Points.push(Data2) ... writeclient.writePoints(Points) try { writeclient.close() //writePoints()后一定要close哦 } catch (e) { logger.error(e) if (e instanceof HttpError && e.statusCode === 401) { logger.error('Run to setup a new InfluxDB database.') } logger.debug('\n influx Finished ERROR') } //下面两行可以打印出Points的内容和将Points转化成LineProtocol的格式,可以拿这个去influxdb UI上在 //line Protocol中测试是否有数据格式错误,比如时间戳的单位是否设置的与写入的一致。时间戳的单位不一致 //不会有error报错。 logger.error(`${Points}`) logger.error(`${Data1.toLineProtocol(writeclient)}`)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3